using System;
using System.Collections.Specialized;
using Microsoft.Extensions.Configuration;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using Topshelf.Demo.Domain;
using Topshelf.Demo.IRepositories;
using Topshelf.Demo.Jobs;
using Topshelf.Logging;

namespace Topshelf.Demo.Services
{
    public class SampleService
    {
        private LogWriter _log;
        private IConfigurationRoot configuration;

        public SampleService(IConfigurationRoot configuration, ISampleRepository sampleRepository)
        {
            _log = HostLogger.Get<SampleService>();
            this.configuration = configuration;

            /*
            NameValueCollection props = new NameValueCollection
            {
                ["quartz.scheduler.instanceName"] = "Topshelf.Demo",
                ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz",
                ["quartz.threadPool.threadPriority"] = "Normal",
                ["quartz.threadPool.threadCount"] = "10",
                ["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins",
                ["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml"
            };
            var sf = new StdSchedulerFactory(props);
             */
            var quartzOption = new QuartzOption(configuration);
            var sf = new StdSchedulerFactory(quartzOption.ToProperties());
            var scheduler = sf.GetScheduler().Result;
            
            scheduler.Start();
            /*
            // 定义一个 Job  
            IJobDetail job = JobBuilder.Create<TestJob>()
                .WithIdentity("job1", "group1")
                .Build();
            ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
                .WithIdentity("trigger1") // 给任务一个名字  
                .StartAt(DateTime.Now) // 设置任务开始时间  
                .ForJob("job1", "group1") //给任务指定一个分组  
                .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(1)  //循环的时间 1秒1次 
                .RepeatForever())
                .Build();


            // 等待执行任务  
            scheduler.ScheduleJob(job, trigger);
            */
        }

        public bool Start()
        {
            _log.Info("Service Start");
            return true;
        }

        public bool Stop()
        {
            _log.Info("Service Stop");
            return true;
        }
    }
}
